package com.besall.allbase.view.activity.chipstoollevel4.protocol.Settings;

import androidx.annotation.NonNull;
import java.util.ArrayList;
import java.util.List;

import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import android.content.Intent;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListAdapter;
import android.widget.TextView;

import androidx.core.view.ViewCompat;

import bb.lanxing.App;
import bb.lanxing.R;
import bb.lanxing.databinding.ActivityWheelLengthSettingSgBinding;
import bb.lanxing.databinding.XossWheelLenItemBinding;
import bb.lanxing.databinding.unit.UnitConstants;
import bb.lanxing.manager.RemoteSharedPreference;
import bb.lanxing.manager.SPConstant;
import bb.lanxing.mvvm.base.BaseDBActivity;
import bb.lanxing.util.InputHelper;

public class WheelLengthSettingActivity extends BaseDBActivity<ActivityWheelLengthSettingSgBinding> {
    private WheelAdapter adapter;
    private boolean isNeedSave;
    private float wheelDiameterLength;
    private List<WheelLength> wheelLengths = new ArrayList<>();
    private List<WheelLength> allWheels = new ArrayList();
    private int wheelSize = 0;

    @Override
    public int getLayoutId() {
        return R.layout.activity_wheel_length_setting_sg;
    }

    @Override
    public void initView(ActivityWheelLengthSettingSgBinding activityWheelLengthSettingSgBinding) {
        setupActionBar(true);
        boolean booleanExtra = false;// getIntent().getBooleanExtra(Debug.DEBUG_PROPERTY_VALUE_AUTO, false);
        this.isNeedSave = getIntent().getBooleanExtra("isNeedSave", true);
        int intExtra = getIntent().getIntExtra("size", 0);
        this.wheelSize = intExtra;
        if (intExtra == 0) {
            this.wheelSize = (int) (RemoteSharedPreference.getInstance().getFloat(SPConstant.KEY_WHEEL_LENGTH, 2.171f)
                    * 1000.0f);
        }
        TextView textView = binding.tvCurrentLength;
        textView.setText(this.wheelSize + UnitConstants.UNIT_SIZE_MM);
        if (booleanExtra) {
            initWheelMap();
            binding.llInput.setVisibility(View.GONE);
            binding.tvInputTips.setVisibility(View.GONE);
            return;
        }
        setTitle(R.string.st_tire_circumference);
        binding.listView.setVisibility(View.GONE);
        App.getContext().getHandler().postDelayed(() -> InputHelper.showSoftInput(binding.inputView), 200L);
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        finish();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_done, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        if (item.getItemId() == R.id.menu_item_done) {
            String obj = ((ActivityWheelLengthSettingSgBinding) this.binding).inputView.getText().toString();
            InputHelper.hideSoftInput(((ActivityWheelLengthSettingSgBinding) this.binding).inputView);
            if (!TextUtils.isEmpty(obj)) {
                try {
                    float parseInt = Integer.parseInt(obj) / 1000.0f;
                    this.wheelDiameterLength = parseInt;
                    if (this.isNeedSave) {
                        RemoteSharedPreference.getInstance().setValue(SPConstant.KEY_WHEEL_LENGTH,
                                Float.valueOf(parseInt));
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            Intent intent = new Intent();
            intent.putExtra("wheelDiameterLength", this.wheelDiameterLength);
            setResult(-1, intent);
            finish();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    private void initWheelMap() {
        this.adapter = new WheelAdapter(this.wheelLengths);
        ((ActivityWheelLengthSettingSgBinding) this.binding).listView.setAdapter((ListAdapter) this.adapter);
        try {
            for (Element element : new SAXReader().read(getAssets().open("tire.xml")).getRootElement()
                    .elements("dict")) {
                Element element2 = element.element("len");
                Element element3 = element.element("des");
                this.allWheels.add(new WheelLength(Integer.parseInt(element2.getText()), element3.getText()));
            }
            this.wheelLengths.addAll(this.allWheels);
            this.adapter.notifyDataSetChanged();
        } catch (Exception e) {
            e.printStackTrace();
        }
        int i = (int) (RemoteSharedPreference.getInstance().getFloat(SPConstant.KEY_WHEEL_LENGTH, 2.171f) * 1000.0f);
        for (int i2 = 0; i2 < this.wheelLengths.size(); i2++) {
            if (this.wheelLengths.get(i2).getLen() == i) {
                this.adapter.setSelected(i2);
            }
        }
    }

    public class WheelAdapter extends BaseAdapter {
        private List<WheelLength> list;
        private int selected;

        WheelAdapter(List<WheelLength> lenghts) {
            this.list = lenghts;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public int getCount() {
            return this.list.size();
        }

        @Override
        public Object getItem(int position) {
            return this.list.get(position);
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            ViewHolder viewHolder;
            if (convertView == null) {
                convertView =
                        LayoutInflater.from(parent.getContext()).inflate(R.layout.xoss_wheel_len_item, parent, false);
                viewHolder = new ViewHolder(convertView);
                convertView.setTag(viewHolder);
            } else {
                viewHolder = (ViewHolder) convertView.getTag();
            }
            WheelLength wheelLength = this.list.get(position);
            TextView textView = viewHolder.binding.lengthView;
            textView.setText(wheelLength.getLen() + "");
            viewHolder.binding.descriptionView.setText(wheelLength.getDes());
            if (position == this.selected) {
                viewHolder.binding.containerView.setBackgroundResource(R.color.global_yellow_color);
                viewHolder.binding.descriptionView.setTextColor(-1);
                viewHolder.binding.lengthView.setTextColor(-1);
            } else {
                viewHolder.binding.containerView.setBackgroundResource(R.color.white);
                viewHolder.binding.descriptionView.setTextColor(ViewCompat.MEASURED_STATE_MASK);
                viewHolder.binding.lengthView.setTextColor(ViewCompat.MEASURED_STATE_MASK);
            }
            viewHolder.binding.containerView.setOnClickListener(v -> WheelAdapter.this.setSelected(position));
            return convertView;
        }

        public void setSelected(int selected) {
            this.selected = selected;
            notifyDataSetChanged();
            WheelLength wheelLength = this.list.get(selected);
            WheelLengthSettingActivity.this.wheelDiameterLength = wheelLength.getLen() / 1000.0f;
            RemoteSharedPreference.getInstance().setValue(SPConstant.KEY_WHEEL_LENGTH,
                    Float.valueOf(wheelLength.getLen() / 1000.0f));
        }
    }

    class ViewHolder {
        XossWheelLenItemBinding binding;

        public ViewHolder(View v) {
            this.binding = XossWheelLenItemBinding.bind(v);
        }
    }

    public class WheelLength {
        private String des;
        private int len;

        WheelLength(int len, String des) {
            this.len = len;
            this.des = des;
        }

        public int getLen() {
            return this.len;
        }

        public String getDes() {
            return this.des;
        }

        public boolean equals(Object o) {
            return (o instanceof WheelLength) && ((WheelLength) o).len == this.len;
        }
    }
}
